package com.kingtone.rongji.tool;

import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * SharedPreferences������
 * 
 * @author zouyb
 * 
 */
public class SharedPreferencesUtil {
	
	private String TAG = "com.kingtone.rongji";
	
	private SharedPreferences sp;
	
	private Editor editor;

	private int mode = Context.MODE_PRIVATE;
	
	private static SharedPreferencesUtil instance;

	
	public static SharedPreferencesUtil getInstance(Context context)
	{
		if(instance == null)
		{
			instance = new SharedPreferencesUtil(context);
		}
		return instance;
	}
	
	public SharedPreferencesUtil(Context context) {
		this.sp = context.getSharedPreferences(TAG, mode);
		this.editor = sp.edit();
	}

	public SharedPreferencesUtil(Context context, String name, int mode) {
		this.sp = context.getSharedPreferences(name, mode);
		this.editor = sp.edit();
	}
	
	public void add(Map<String, String> map) {
		Set<String> set = map.keySet();
		for (String key : set) {
			editor.putString(key, map.get(key));
		}
		editor.commit();
	}
	
	public void putString(String key, String value) {
		editor.putString(key, value);
		editor.commit();
	}
	
	public void putBoolean(String key, boolean value) {
		editor.putBoolean(key, value);
		editor.commit();
	}

	public void deleteAll() throws Exception {
		editor.clear();
		editor.commit();
	}

	public void delete(String key) {
		editor.remove(key);
		editor.commit();
	}

	public String getString(String key) {
		if (sp != null) {
			return sp.getString(key, "");
		}
		return "";
	}
	
	public boolean getBoolean(String key) {
		if (sp != null) {
			return sp.getBoolean(key, false);
		}
		return false;
	}

	public Editor getEditor() {
		return editor;
	}
}
